package com.zhuss.product.processor;

import com.zhuss.bean.bo.product.site.StartSiteBo;
import com.zhuss.bean.vo.product.site.DetailSiteVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.StartStopEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.SiteDao;
import com.zhuss.product.db.entity.Site;
import com.zhuss.product.processor.cache.SiteCache;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author summer
 * @date 2021/1/6 8:43
 */
@Component("siteStart")
public class SiteStart extends AbstractProcessor<Object, StartSiteBo> implements Processor<Object, StartSiteBo> {

    @Autowired
    private SiteDao siteDao;

    @Autowired
    @Qualifier("siteRead")
    private SiteRead siteRead;

    @Autowired
    @Qualifier("siteCache")
    private SiteCache siteCache;


    @Override
    public void check(StartSiteBo startSiteBo) throws DzcxException {
        DetailSiteVo siteVo = (DetailSiteVo) siteRead.detail(startSiteBo.getSiteId());
        if (StartStopEnum.启用.sameKey(siteVo.getStatus())) {
            throw new DzcxException(ExceptionCode.当前数据不满足启用条件);
        }
    }

    @Override
    public Object process(StartSiteBo param) throws DzcxException {
        Long siteId = param.getSiteId();

        Site record = new Site();
        record.setSiteId(siteId);
        record.setStatus(param.getStatus());

        record.setModifierId(param.getConcurrentUserId());
        record.setModifier(param.getConcurrentUserName());
        record.setModifyTime(new Date());
        siteDao.updateByPrimaryKeySelective(record);

        String name = ((DetailSiteVo) siteRead.detail(siteId)).getName();
        param.setName(name);

        siteCache.clear(siteId);
        return null;
    }

    @Override
    public Object log(StartSiteBo param) throws DzcxException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.商品.key());
        record.setAction(LogActionEnum.站点启用.key());
        record.setTargetId(param.getConcurrentUserId());
        record.setTargetName(param.getName());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());
        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }
}
